#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import urllib
import os
import sys


def get_addr_list(url):
    web_content = urllib.urlopen(url).read()
    web_content.decode('gbk', 'ignore')
    line = re.findall("4u-a \d{4} \d{4}.mp3", web_content)
    return list(set(line))


def auto_down_list(linklist):
    print 'begin to download...'
    for i in linklist:
        addr = 'http://www.listeningexpress.com/xl3/talk/4u/' + i
        try:
            fname = os.path.join(r'c:/temp', i)
            urllib.urlretrieve(addr, fname)
            print 'OK  %s' % fname
        except:
            print 'Not downloaded!'


if __name__ == '__main__':
    aa = get_addr_list('http://www.listeningexpress.com/4u/a/')

    print auto_down_list(aa)
